package database;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.BorderLayout;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Select extends JInternalFrame {

	Connection conn;
	DatabaseTest dt;
	
	private JButton button;
	private JScrollPane scrollPane;
	private JTable table;
		
	DefaultTableModel dtm=null;
	Vector colName;
	Vector rowdata;
	private JPanel panel;
	private JTextField sqlIn;
	private JButton btnNewButton;
	private JLabel status;
	
	
	public Select(Connection c, DatabaseTest d) {
		super("조회",true,true,true,false);
		this.conn=c;
		this.dt=d;
		setBounds(0, 200, 682, 286);
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getStatus(),BorderLayout.SOUTH);

	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("Search All");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					searchAll();
					
					
					
					
				}
			});
		}
		return button;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					
					if(dt.update == null){
						dt.openUpdate();
					}
					
					int row = table.getSelectedRow();
					String se = String.valueOf(table.getValueAt(row, 1));
					String co = (String)table.getValueAt(row, 2);
					String ea = String.valueOf(table.getValueAt(row, 3));
										
					dt.update.setData(se, co, ea);
					dt.update.setVisible(true);
					dt.update.toFront();
					dt.update.serialIn.setEnabled(false);
					
				}
			});
		}
		return table;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getButton(), BorderLayout.SOUTH);
			panel.add(getSqlIn(), BorderLayout.CENTER);
			panel.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JTextField getSqlIn() {
		if (sqlIn == null) {
			sqlIn = new JTextField();
			sqlIn.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode() == e.VK_ENTER){
						setList();
					}
				}
			});
			sqlIn.setColumns(10);
		}
		return sqlIn;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("Execute");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					setList();
					
					
				}
			});
		}
		return btnNewButton;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("SQL 문을 검색해주세요.");
			status.setBounds(12, 146, 160, 15);
		}
		return status;
	}
	public void setList(){
		// 강사님 소스
		String sq = sqlIn.getText();
		try{
			PreparedStatement pst = conn.prepareStatement(sq);
			ResultSet rs = pst.executeQuery();
			ResultSetMetaData rsm = pst.getMetaData();
			Vector v = new Vector();
			v.add("No");
			
			for(int i=1; i<=rsm.getColumnCount();i++){
				v.add(rsm.getColumnName(i));
			}
			
			dtm = new DefaultTableModel(v,0);
			table.setModel(dtm);
			int cnt = 1;
			
			while(rs.next()){
				Vector d = new Vector<>();
				d.add(cnt);
				for(int i =1 ; i<=rsm.getColumnCount();i++){
					d.add(rs.getString(i));
				}
				
				dtm.addRow(d);
				cnt++;
			}
			
			
		}catch(Exception exx){
			//exx.printStackTrace();
			status.setText("정확한 sql 문장을 입력해주세요.");
		}
		
	}
	
	public void searchAll(){
		ResultSet rs = null;
		try{
			
			String[] col = {"No","Serial","Code","Ea"};
			dtm = new DefaultTableModel(col,0);
			table.setModel(dtm);					
			
			String sql = "select * from ptbl";
			
			PreparedStatement pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			
			
			int cnt=1; // 테이블의 NO에 들어갈 int 변수
			
			
			while(rs.next()){
				rowdata = new Vector();
				rowdata.add(cnt);
				rowdata.add(rs.getInt("serial"));
				rowdata.add(rs.getString("code"));
				rowdata.add(rs.getInt("ea"));							
				
				dtm.addRow(rowdata);
				cnt++;
			}
			
			status.setText("DB, ptbl 테이블이 검색되었습니다.");
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
